<?php
/**
 * 平台统计
 * User: Administrator
 * Date: 2018/10/19
 * Time: 14:03
 */

namespace app\admin\controller;

use think\Db;
class Stati    extends Init
{
    //平台订单统计报表
    function index(){
        if ($this->request->isAjax()) {
            $key = input('key')?input('key'):'';
            $sj = input('sj')?input('sj'):'';

            if(is_numeric(input('materiel_id')) === true){
                $where['o.materiel_id'] = input('materiel_id');
                $materielId = Db::table('je_order_goods')->alias('g')->join('je_goods o','g.goods_id = o.goods_id')->field('g.order_id,o.materiel_id')->where($where)->select();
                $m = array_column($materielId,'order_id');
                unset($where['o.materiel_id']);
                $where['o.order_id'] = array('in',$m);
            }


            if(!empty($sj)){
                $sj = explode(' - ',$sj);
                $where['o.add_time'] = array(array('egt',strtotime($sj[0])),array('elt',strtotime($sj[1]))) ;
            }
            $where['u.name|o.order_sn|s.name'] = array('like','%'.trim($key).'%');

            $where['o.order_status'] = 3;
            $list = Db::table('je_order')->alias('o')->join('je_user u','o.user_id = u.user_id')->join('je_store s','o.store_id = s.store_id')->field('o.order_id,o.order_sn,o.pay_price,o.type,o.end_time,o.add_time,o.business_settlement_price,o.commission,u.name as sname,s.name as stname')->order('order_id desc')->where($where)->paginate(input('limit'))->toArray();

            foreach($list['data'] as $k=>$item){
                $list['data'][$k]['lr'] = 0;
                if($item['type']==1){
                    $list['data'][$k]['lr'] = $item['pay_price']-($item['business_settlement_price']*$item[''])-$item['commission'];
                }elseif($item['type']==2){

                }
                $list['data'][$k]['lr'] = $item['pay_price']-($item['business_settlement_price'])-$item['commission'];
            }

            return json($result = ['code'=>0,'msg'=>'获取成功','data'=>$list['data'],'count'=>$list['total']]);
        }

        $materiel = db('materiel')->where('parent_id=0')->field('materiel_id,name,parent_id')->select();
        foreach ($materiel as $key=>$itme){
            $materiel[$key]['mat'] = db('materiel')->where("parent_id = {$itme['materiel_id']}")->field('materiel_id,name,parent_id')->select();
        }

        return view('list',['list'=>$materiel]);
    }




    //分销佣金
    function index2(){
        if ($this->request->isAjax()) {
            $key = input('key')?input('key'):'';
            $sj = input('sj')?input('sj'):'';

//            if(is_numeric(input('type2')) === true){
//              $where['o.distribution_settlement'] = input('type2');
//            }

            if(!empty($sj)){
                $sj = explode(' - ',$sj);
                $where['add_time'] = array(array('egt',strtotime($sj[0])),array('elt',strtotime($sj[1]))) ;
            }
            $where['u.name|u.phone'] = array('like','%'.trim($key).'%');
//
//            $where['o.order_status'] = 3;
//            $where['o.type'] = 2;


            $list = Db::table('je_commissions')->alias('c')->join('je_user u','c.suser_id = u.user_id')->order('commissions_id desc')->field('c.*,u.name as uname,u.phone')->where($where)->paginate(input('limit'))->toArray();

            foreach ($list['data'] as $key=>$itme){
                if($itme['type']==1){

                    $order = db('order')->field('order_status,pay_price,distribution_settlement,add_time,end_time,order_sn')->where("order_id={$itme['order_id']}")->find();
                    if($order['order_status']==1){
                        $list['data'][$key]['order_status'] = '已付款';
                    }elseif($order['order_status']==2){
                        $list['data'][$key]['order_status'] = '已发货';
                    }elseif($order['order_status']==3){
                        $list['data'][$key]['order_status'] = '已完成';
                    }

                    $list['data'][$key]['oadd_time'] = $order['add_time'];
                    $list['data'][$key]['pay_price'] = $order['pay_price'];
                    $list['data'][$key]['end_time'] = $order['end_time'];
                    $list['data'][$key]['distribution_settlement'] = $order['distribution_settlement'];
                    $list['data'][$key]['order_sn'] = $order['order_sn'];


                }elseif ($itme['type']==2){
                    $card_order = db('card_order')->field('add_time,pay_time,pay_price,card_order_sn')->where("card_order_id={$itme['order_id']}")->find();
                    $list['data'][$key]['order_sn'] = $card_order['card_order_sn'];
                    $list['data'][$key]['oadd_time'] = $card_order['add_time'];
                    $list['data'][$key]['pay_price'] = $card_order['pay_price'];
                    $list['data'][$key]['end_time'] = $card_order['pay_time'];
                    $list['data'][$key]['distribution_settlement'] = 1;
                    $list['data'][$key]['order_status'] = '已完成';
                }
                if(!empty($itme['upper_level_id'])){
                    $list['data'][$key]['uuname'] =  db('user')->where('user_id',$itme['upper_level_id'])->value('name');
                }else{
                    $list['data'][$key]['uuname'] =  '无';
                }
                $list['data'][$key]['uanme'] = db('user')->where('user_id',$itme['user_id'])->value('name');
            }

            return json($result = ['code'=>0,'msg'=>'获取成功','data'=>$list['data'],'count'=>$list['total']]);
        }


        return view('list2');
    }


    //共享订单统计
    function index3(){
        if ($this->request->isAjax()) {
            $key = input('key')?input('key'):'';
            $sj = input('sj')?input('sj'):'';


            if(!empty($sj)){
                $sj = explode(' - ',$sj);
                $where['o.add_time'] = array(array('egt',strtotime($sj[0])),array('elt',strtotime($sj[1]))) ;
            }
            $where['u.name|u.phone|o.order_sn|s.name'] = array('like','%'.trim($key).'%');

            $where['o.order_status'] = array('in',[1,2,3,5,6,7,8,9,10]);
            if(is_numeric(input('type')) === true){
                $where['o.order_status'] = input('type');
                if(input('type')==6){
                    $where['o.order_status'] = array('in',[3,6,7,8,9]);
                }
            }
            $where['o.type'] = 1;
            $list = Db::table('je_order')->alias('o')->join('je_user u','o.user_id = u.user_id')->join('je_store s','o.store_id = s.store_id')->field('o.abrasion_price,o.pay_price,o.order_status,o.goods_price,o.overdue_price,o.order_id,o.order_sn,o.pay_price,o.type,o.commission,o.distribution_settlement,o.end_time,o.add_time,o.business_settlement_price,o.commission,u.name as uname,u.phone,s.name as stname')->order('order_id desc')->where($where)->paginate(input('limit'))->toArray();


            foreach ($list['data'] as $key=>$itme){
                $list['data'][$key]['goods'] = Db::table('je_order_goods')->alias('i')->join('je_goods g','i.goods_id = g.goods_id')->field('g.name,i.*')->where("order_id={$itme['order_id']}")->select();
                $list['data'][$key]['abrasion_price'] = $itme['overdue_price']+$itme['abrasion_price'];

                if($itme['order_status']==0){
                    $list['data'][$key]['order_status'] = '未付款';
                }elseif ($itme['order_status']==1){
                    $list['data'][$key]['order_status'] = '已付款';
                }elseif ($itme['order_status']==2){
                    $list['data'][$key]['order_status'] = '已发货';
                }elseif ($itme['order_status']==3){
                    $list['data'][$key]['order_status'] = '已完成';
                }elseif ($itme['order_status']==4){
                    $list['data'][$key]['order_status'] = '已取消';
                }elseif ($itme['order_status']==5){
                    $list['data'][$key]['order_status'] = '归还中';
                }elseif ($itme['order_status']==6){
                    $list['data'][$key]['order_status'] = '店铺已收货';
                }elseif ($itme['order_status']==7){
                    $list['data'][$key]['order_status'] = '已验收';
                }elseif ($itme['order_status']==8){
                    $list['data'][$key]['order_status'] = '待用户确认';
                }elseif ($itme['order_status']==9){
                    $list['data'][$key]['order_status'] = '待用户支付';
                }elseif ($itme['order_status']==10){
                    $list['data'][$key]['order_status'] = '租赁中';
                }

            }

            return json($result = ['code'=>0,'msg'=>'获取成功','data'=>$list['data'],'count'=>$list['total']]);
        }
        return view('list3');
    }


    //分类共享统计
    function index4(){
        if ($this->request->isAjax()) {
            $key = input('key')?input('key'):'';
            $sj = input('sj')?input('sj'):'';

            if(is_numeric(input('materiel_id')) === true){
                $where['o.materiel_id'] = input('materiel_id');
                $materielId = Db::table('je_order_goods')->alias('g')->join('je_goods o','g.goods_id = o.goods_id')->field('g.order_id,o.materiel_id')->where($where)->select();
                $m = array_column($materielId,'order_id');
                unset($where['o.materiel_id']);
                $where['o.order_id'] = array('in',$m);
            }


            if(!empty($sj)){
                $sj = explode(' - ',$sj);
                $where['o.add_time'] = array(array('egt',strtotime($sj[0])),array('elt',strtotime($sj[1]))) ;
            }
            $where['u.name|o.order_sn|s.name'] = array('like','%'.trim($key).'%');

            $where['o.order_status'] = 3;
            $list = Db::table('je_order')->alias('o')->join('je_user u','o.user_id = u.user_id')->join('je_store s','o.store_id = s.store_id')->field('o.order_id,o.order_sn,o.pay_price,o.type,o.confirm_time,o.add_time,o.business_settlement_price,o.commission,u.name as sname,s.name as stname')->order('order_id desc')->where($where)->paginate(input('limit'))->toArray();

            foreach($list['data'] as $k=>$item){
                $list['data'][$k]['lr'] = 0;
                $list['data'][$k]['lr'] = $item['pay_price']-$item['business_settlement_price']-$item['commission'];
            }

            return json($result = ['code'=>0,'msg'=>'获取成功','data'=>$list['data'],'count'=>$list['total']]);
        }

        $materiel = db('materiel')->where('parent_id=0')->field('materiel_id,name,parent_id')->select();
        foreach ($materiel as $key=>$itme){
            $materiel[$key]['mat'] = db('materiel')->where("parent_id = {$itme['materiel_id']}")->field('materiel_id,name,parent_id')->select();
        }

        return view('list4',['list'=>$materiel]);
    }



    //共享押金统计
    function index5(){
        if ($this->request->isAjax()) {
            $key = input('key')?input('key'):'';
            $sj = input('sj')?input('sj'):'';
            if(!empty($sj)){
                $sj = explode(' - ',$sj);
                $where['o.add_time'] = array(array('egt',strtotime($sj[0])),array('elt',strtotime($sj[1]))) ;
            }
            $where['u.name|o.order_sn|s.name'] = array('like','%'.trim($key).'%');

            $where['o.order_status'] = 3;
            $where['o.type'] = 1;
            $where['o.deposit_price'] = array('gt',0.00);
            $list = Db::table('je_order')->alias('o')->join('je_user u','o.user_id = u.user_id')->join('je_store s','o.store_id = s.store_id')->field('o.refund_price,o.order_id,o.order_sn,o.deposit_price,o.type,o.confirm_time,o.add_time,o.business_settlement_price,o.commission,u.name as sname,s.name as stname')->order('order_id desc')->where($where)->paginate(input('limit'))->toArray();

            return json($result = ['code'=>0,'msg'=>'获取成功','data'=>$list['data'],'count'=>$list['total']]);
        }

        return view('list5');
    }



    //会员卡购买统计
    function index6(){
        if ($this->request->isAjax()) {
            $key = input('key')?input('key'):'';
            $sj = input('sj')?input('sj'):'';

            if(is_numeric(input('type')) === true){
                $where['c.type'] = input('type');
            }

            if(is_numeric(input('type2')) === true){
                $where['m.type'] = input('type2');
            }
            if(is_numeric(input('channel')) === true){
                $where['c.channel'] = input('channel');
            }
            if(!empty($sj)){
                $sj = explode(' - ',$sj);
                $where['c.add_time'] = array(array('egt',strtotime($sj[0])),array('elt',strtotime($sj[1]))) ;
            }
            $where['u.name|c.card_order_sn|m.name|u.phone'] = array('like','%'.trim($key).'%');
            $where['c.card_status'] = 1;
            $list = Db::table('je_card_order')->alias('c')
                ->join('je_membership_card m','c.card_id = m.card_id')
                ->join('je_user u','c.user_id = u.user_id')
                ->field('u.name as uname,u.phone,c.*,m.name as mname,m.type as mtype')->where($where)->paginate(input('limit'))->toArray();;


            return json($result = ['code'=>0,'msg'=>'获取成功','data'=>$list['data'],'count'=>$list['total']]);
        }

        return view('list6');
    }

}